package finedev.android.venue;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class NewVideoActivity extends NetworkActivity {
	
	private Activity mActivity = null;
	
	private final String TAG = "newvideo";
	
	public static HashMap<String, String> mNewVideoResult = null;
	
	private ListView mListView;
	private NewVideoAdapter mNewVideoAdapter;
	private ArrayList<VideoItem> mVideoListData = new ArrayList<VideoItem>();
	
	private int mRequestMaxAdKey = 10000;
	private int mRequestCount = 15;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newvideo);
        
        mActivity = this;
        
        mListView = (ListView) findViewById(R.id.newvideo_listview);
        mNewVideoAdapter = new NewVideoAdapter( mActivity, R.layout.newvideo_row, mVideoListData );
        mListView.setAdapter( mNewVideoAdapter );
        mListView.setOnItemClickListener( itemClickListener );
        
        GetNewVideo();
    }
    
    private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long l_position) {
			Intent intent = new Intent ( getApplicationContext(), Detail_NewVideoActivity.class );
			VideoItem videoItem = mVideoListData.get(position);
			intent.putExtra("item", videoItem );
			startActivity( intent );			
		}
    	
    };
    
    private void GetNewVideo(){
    	ProgressDialog progressDialog = new ProgressDialog( mActivity );
		progressDialog.setMessage("get new video...");
		progressDialog.setCancelable(false);
		
		NetworkManager newvideoManager = new NetworkManager( mActivity, progressDialog, "newvideo" );
		newvideoManager.execute( "http://www.vennue.net/new/m_newvideo.php", 
			String.valueOf(mRequestMaxAdKey),
			String.valueOf(mRequestCount) );
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_friend, menu);
        return true;
    }

	@SuppressWarnings("unchecked")
	@Override
	public void ReceiveSuccess(String str) {
		Log.i(TAG, "ReceiveSuccess(String str) : " + str );
		
		try {
			JSONArray jsonArray = new JSONArray( str );
			Log.i(TAG, "jsonarray length : " + jsonArray.length() );
			for ( int i=0; i<jsonArray.length(); i++ ) {
				JSONObject jsonObject = jsonArray.getJSONObject( i );
				mVideoListData.add( new VideoItem( jsonObject.getString("ad_key"), jsonObject.getString("ad_title"), jsonObject.getString("ad_content"), jsonObject.getString("ad_movieurl"), jsonObject.getString("ad_link"), jsonObject.getString("ad_ownerkey"), jsonObject.getString("ad_hit"), jsonObject.getString("uv_hit"), "http://www.vennue.net/new/" + jsonObject.getString("ad_thumbnail") ));
			}
			
			mNewVideoAdapter.notifyDataSetChanged();
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void ReceiveFail(String str) {
		// TODO Auto-generated method stub
		
	}
}
